import { Component, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BopContentServiceProxy, BopDescServiceProxy, BopNodeEditDto, BopNodeServiceProxy, GetBopRelationInput, } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { HttpClient } from '@angular/common/http';


@Component({
    selector: 'createOrEditDrawModal',
    templateUrl: './create-or-edit-draw-modal.component.html',
})
export class CreateOrEditDrawModalComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() parentrow: any;//当前点击节点
    @Input() bopInfo: any;//当前bopInfo
    saving = false;
    active = false;
    filterText = '';
    selectedCar: any;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _bopContentServiceProxy: BopContentServiceProxy,
        private _messageService: MessageService,
        private _httpClient: HttpClient
    ) {
        super(injector);
        // this.filterText = this._bopContentServiceProxy.snapshot.queryParams['filterText'] || '';
    }
    //导入工艺规程
    show(): void {
        this.active = true;

        this.modal.show();

    }
    //获取工艺规程文件
    getProcessDraw(event?: LazyLoadEvent) {
        let that = this;
        setTimeout(() => {
            if (that.primengTableHelper.shouldResetPaging(event)) {
                that.paginator.changePage(0);
                return;
            }
            that.primengTableHelper.showLoadingIndicator();
            that._bopContentServiceProxy.getProcessFileList(
                that.filterText,
                that.primengTableHelper.getSorting(that.dataTable),
                that.primengTableHelper.getMaxResultCount(that.paginator, event),
                that.primengTableHelper.getSkipCount(that.paginator, event)
            ).pipe(finalize(() => that.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                that.primengTableHelper.totalRecordsCount = result.totalCount;
                that.primengTableHelper.records = result.items;
                that.primengTableHelper.hideLoadingIndicator();
            });
        }, 10);


    }
    //关闭事件
    close(): void {
        this.active = false;
        this.selectedCar = undefined;
        this.modal.hide();
    }
    //导入工艺规程
    save() {
        if (!this.selectedCar) {
            this._messageService.info('请选择工艺规程文件');
            return;
        }
        let params = new GetBopRelationInput();
        params.nodeId = this.parentrow.id;
        params.techniqueEntityId = this.selectedCar.id;
        params.techniqueNumber = this.selectedCar.techniqueNumber;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bopContentServiceProxy.bopRelationFile(params)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('导入工艺规程成功');
                        this.close();
                        this.modalSave.emit(null);
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }


}
